package com.yuqing.sentimentapp.yqlist.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.home.bean.ReplyBean;
import com.yuqing.sentimentapp.home.bean.ReplyDataBean;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.utils.ToastUtil;
import com.yuqing.sentimentapp.yqlist.adapter.YqReportAdapter;
import com.yuqing.sentimentapp.yqlist.fragment.DisasterTemplateFragment;
import com.yuqing.sentimentapp.yqlist.fragment.OtherTemplateFragment;
import com.yuqing.sentimentapp.yqlist.fragment.RescueTemplateFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ShenHe on 2019/4/7.
 */

public class YQReportActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_yq_title)
    TextView mTvYqTitle;
    @BindView(R.id.tabs)
    XTabLayout mTabs;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    private String[] mMoudleName = {"灾情模板", "救援模板", "其他"};
    private YqReportAdapter mAdapter;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private RescueTemplateFragment mRescueTemplateFragment;
    private DisasterTemplateFragment mDisasterTemplateFragment;
    private OtherTemplateFragment mOtherTemplateFragment;
    private String linkId;
    private String title;
    private String url;
    private String content;
    private String replyId;
    private int position;
    private int state;

    public static void start(Context context) {
        Intent intent = new Intent(context, YQReportActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, String linkId, String title, String url, String content, int position, int state) {
        Intent intent = new Intent(context, YQReportActivity.class);
        intent.putExtra("linkId", linkId);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        intent.putExtra("content", content);
        intent.putExtra("position", position);
        intent.putExtra("state", state);
        context.startActivity(intent);
    }

    public static void start(Context context, String linkId, String title, String url, String content, String replyId, int position, int state) {
        Intent intent = new Intent(context, YQReportActivity.class);
        intent.putExtra("linkId", linkId);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        intent.putExtra("content", content);
        intent.putExtra("replyId", replyId);
        intent.putExtra("position", position);
        intent.putExtra("state", state);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_yq_report;
    }

    @Override
    public void initPresenter() {

    }

    public String getYqLinkId() {
        return linkId;
    }

    public String getYqTitle() {
        return title;
    }

    public String getYqUrl() {
        return url;
    }

    public String getYqContent() {
        return content;
    }

    public int getPosition() {
        return position;
    }

    @Override
    public void initView() {
        initIntent();
        initTitle();
        //初始化frament
        initFragment();
        setViewPager();

    }

    private void initTitle() {
        //设置标题
        if (state == 0) {//上报
            mTvTitle.setText("上报");
        } else {//重报
            mTvTitle.setText("重报");
        }
    }

    private void initIntent() {
        Intent intent = getIntent();
        if (intent == null) return;
        linkId = intent.getStringExtra("linkId");
        title = intent.getStringExtra("title");
        url = intent.getStringExtra("url");
        content = intent.getStringExtra("content");
        replyId = intent.getStringExtra("replyId");
        position = intent.getIntExtra("position", -1);
        state = intent.getIntExtra("state", 0);

        mTvYqTitle.setText(title);

    }

    private void setViewPager() {
        mAdapter = new YqReportAdapter(getSupportFragmentManager(), mFragmentList, Arrays.asList(mMoudleName));
        mViewPager.setAdapter(mAdapter);
        mTabs.setupWithViewPager(mViewPager);

        if (TextUtils.isEmpty(replyId)) {
            return;
        }

        ModuleManager.getInstance().getReplyRequest(this, replyId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ReplyDataBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(ReplyDataBean replysBean) {
                        if (replysBean == null) return;
                        ReplyBean data = replysBean.getData();
                        if (data == null) return;
                        updateReply(data);
                    }

                    @Override
                    protected void onFailure(String message) {
                        ToastUtil.showShort(message);
                    }
                });
    }

    private void updateReply(ReplyBean reply) {
        String replyType = reply.getReplyType();
        if (TextUtils.isEmpty(replyType)) return;
        switch (replyType) {
            case "A":
                mViewPager.setCurrentItem(0);
                mDisasterTemplateFragment.updateData(reply, state);
                break;
            case "B":
                mViewPager.setCurrentItem(1);
                mRescueTemplateFragment.updateData(reply, state);
                break;
            case "C":
                mViewPager.setCurrentItem(2);
                mOtherTemplateFragment.updateData(reply, state);
                break;
            default:
                mViewPager.setCurrentItem(0);
                mDisasterTemplateFragment.updateData(reply, state);
                break;
        }
    }

    private void initFragment() {
        mDisasterTemplateFragment = new DisasterTemplateFragment();
        mRescueTemplateFragment = new RescueTemplateFragment();
        mOtherTemplateFragment = new OtherTemplateFragment();
        mFragmentList.add(mDisasterTemplateFragment);
        mFragmentList.add(mRescueTemplateFragment);
        mFragmentList.add(mOtherTemplateFragment);
    }

    @OnClick(R.id.iv_back)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
